package com.wsoft.portal.utils;

import com.wsoft.core.service.CommonOrgService;
import com.wsoft.entity.BaseEntity;
import com.wsoft.portal.entity.PortalModuleEntity;
import com.wsoft.portal.entity.PortalModuleSubEntity;
import com.wsoft.portal.entity.PortalOrgMemberEntity;
import com.wsoft.portal.manager.PortalModuleManager;
import com.wsoft.portal.manager.PortalModuleSubManager;
import com.wsoft.portal.manager.PortalOrgMemberManager;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author z001
 * @Date 2025/1/2 09:00
 */
@Service
public class PortalUtil {

    @Resource
    private PortalOrgMemberManager orgMemberManager;
    @Resource
    private PortalModuleManager moduleManager;
    @Resource
    private PortalModuleSubManager subManager;
    @Resource
    private CommonOrgService orgService;

    /**
     * 复制
     * @param newThemeId 新增主题的时候 主题id
     * @param themeDefault 复制的上级节点 PortalOrgMemberEntity 对象
     * @param orgId 组织新建的时候传
     * @param memberId 用户新建的时候传
     */
    public PortalOrgMemberEntity copyDefaultTheme(Long newThemeId,PortalOrgMemberEntity themeDefault,Long orgId,Long memberId) {
        if (null == themeDefault) {
            return null;
        }
        Long oldPortalId = themeDefault.getId();
        themeDefault.setThemeId(null == newThemeId ? themeDefault.getId() : newThemeId);
        themeDefault.setSort(themeDefault.getSort());
        themeDefault.setId(null);
        themeDefault.setParentPortalId(oldPortalId);
        //集成上级
        themeDefault.setExtendParent(1);
        if(null != orgId){
            themeDefault.setOrgId(orgId);
        }
        if(null != memberId){
            themeDefault.setMemberId(memberId);
        }
        themeDefault.setCreateBy(null);
        themeDefault.setCreateTime(null);
        themeDefault.setUpdateBy(null);
        themeDefault.setUpdateTime(null);
        orgMemberManager.save(themeDefault);
        Long newPortalId = themeDefault.getId();


        //获取默认模块并复制
        //模块新旧id对应map
        Map<Long, Long> moduleIdMap = new HashMap<>(16);
        List<PortalModuleEntity> moduleEntityList = moduleManager.getByPortalId(oldPortalId);
        List<Long> moduleOldIdList = moduleEntityList.stream().map(BaseEntity::getId).collect(Collectors.toList());
        List<PortalModuleEntity> moduleCollect = moduleEntityList.stream().peek(item -> {
            item.setId(null);
            item.setCreateBy(null);
            item.setCreateTime(null);
            item.setUpdateBy(null);
            item.setUpdateTime(null);
            item.setPortalId(newPortalId);
        }).collect(Collectors.toList());
        moduleManager.saveBatch(moduleCollect);
        for (int i = 0; i < moduleCollect.size(); i++) {
            moduleIdMap.put(moduleOldIdList.get(i), moduleCollect.get(i).getId());
        }

        //获取sub表 并复制
        List<PortalModuleSubEntity> subEntityList = subManager.getParIdList(moduleOldIdList);
        List<PortalModuleSubEntity> subCollect = subEntityList.stream().peek(item -> {
            item.setId(null);
            item.setCreateBy(null);
            item.setCreateTime(null);
            item.setUpdateBy(null);
            item.setUpdateTime(null);
            item.setPortalModuleId(moduleIdMap.get(item.getPortalModuleId()));
        }).collect(Collectors.toList());
        subManager.saveBatch(subCollect);

        return themeDefault;
    }

    /**
     * 获取上级
     * @param themeId
     * @param curOrgId
     * @return
     */
    public PortalOrgMemberEntity getParentPortal(Long themeId,Long  curOrgId){
        //递归获取上级节点
        List<Long> parOrgId = orgService.getParOrgId(curOrgId, false);
        PortalOrgMemberEntity parPortal = getParPortal(themeId, parOrgId);
        if(null == parPortal){
            //如果递归后最上层还是为空,则复制默认主题
            parPortal = orgMemberManager.getThemeDefault(themeId);
        }
        return parPortal;
    }


    /**
     * 修改是否继承父级
     * @param portalId
     */
    public void updateExtendParent(Long portalId){
        PortalOrgMemberEntity entity = orgMemberManager.getById(portalId);
        if(null == entity || null == entity.getExtendParent() ||  0 == entity.getExtendParent()){
            return;
        }
        entity.setExtendParent(0);
        orgMemberManager.updateById(entity);
    }

    /**
     * 修改是否继承父级 ==> 用户sub表的时候
     * @param portalModuleId
     */
    public void updateExtendParentSub(Long portalModuleId){
        PortalModuleEntity entity = moduleManager.getById(portalModuleId);
        updateExtendParent(entity.getPortalId());
    }


    // ################################## 私有方法  ##################################
    /**
     * 获取上级portal
     * @param themeId
     * @param parOrgId
     * @return
     */
    private PortalOrgMemberEntity getParPortal(Long themeId, List<Long> parOrgId) {
        for (Long orgId : parOrgId) {
            PortalOrgMemberEntity portal = orgMemberManager.getByThemeId(themeId, orgId, null);
            if (null != portal) {
                return portal;
            }
        }
        return null;
    }
}
